#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 1.5e5 + 10, M = 1e7 + 10;
int h[N], e[M], ne[M], idx;
int f[N];
int p[N];
int n1, n2, m;
int st[N];
int find(int x)
{
	if (x != p[x])p[x] = find(p[x]);
	return p[x];
}

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void bfs(int u, int fa)
{
	queue<PII>q;
	q.push({ u,fa });
	while (q.size()) {
		PII t = q.front();
		q.pop();
		int u = t.x;
		int v = t.y;
		st[u] = 1;
		for (int i = h[u]; ~i; i = ne[i]) {
			int j = e[i];
			if (j == v)continue;
			if (!st[j]) {
				if (!f[j]) {
					f[j] = f[u] + 1;
					q.push({ j,u });
				}
			}
			else break;
		}
	}
}
int main()
{
	cin >> n1 >> n2 >> m;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n1 + n2; i++)p[i] = i;
	while (m--) {
		int a, b;
		cin >> a >> b;
		int a1 = find(a);
		int b1 = find(b);
		add(a, b);
		add(b, a);
		if (a1 != b1) {
			p[a1] = b1;
		}
	}
	int idx1 = find(n1);
	int idx2 = find(n2);
	//cout << "idx1==" << idx1 << endl;
	//cout << "idx2==" << idx2 << endl;
	bfs(idx1, 0);
    bfs(idx2, 0);
	int ans1 = 0, ans2 = 0;
	for (int i = 1; i <= n1; i++) {
		//cout << "f[i]==" << f[i] << endl;
		ans1 = max(ans1, f[i]);
	}
	for (int i = n1 + 1; i <= n1 + n2; i++) {
		//cout << "f[i]==" << f[i] << endl;
		ans2 = max(ans2, f[i]);
	}
	//cout << "ans1==" << ans1 << endl;
	//cout << "ans2==" << ans2 << endl;
	cout << ans1 + ans2 + 1 << endl;
	return 0;
}